//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include "general.h"
#include <buola/io.h>

CDataServer::CDataServer()
    :   dbus::CObject<CDataServer>("/org/buola/BigBrother/Data")
{
    mRoot=new CRootBranch;
    mRoot->AddBranch("calendars",new CVirtualBranch);
    mRoot->AddBranch("contacts",new CVirtualBranch);
    mRoot->AddBranch("config",new CVirtualBranch);
}

CDataServer::~CDataServer()
{
}

std::pair<CBBBranch*,std::string> CDataServer::FindBranch(const std::string &pPath)
{
    if(pPath.empty()) return std::make_pair((CBBBranch*)nullptr,"");

    if(pPath[0]=='/') //real path
    {
        if(pPath.size()==1) return std::make_pair(mRoot,"");
        return mRoot->FindBranch(pPath,1);
    }
    else
    {
        //TODO:numeric path?
        return std::make_pair((CBBBranch*)nullptr,"");
    }
}

std::vector<std::string> CDataServer::GetLeafList(const std::string &pBranch)
{
    std::pair<CBBBranch*,std::string> lBranch=FindBranch(pBranch);

    std::vector<std::string> lList;
    msg_info() << lBranch.second << "\n";
    lBranch.first->GetLeafList(lBranch.second,lList);

    return lList;
}

UValue CDataServer::GetLeafValue(const std::string& /*pBranch*/,const std::string& /*pLeaf*/)
{
    ///\todo implement this
    return UValue();
}
